package com.simple.quartz.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.simple.framework.core.domain.vo.PagingVo;
import com.simple.quartz.domain.dto.ListSysJobLogDto;
import com.simple.quartz.domain.entity.SysJobLog;
import com.simple.quartz.mapper.SysJobLogMapper;
import com.simple.quartz.service.SysJobLogService;
import org.springframework.stereotype.Service;

/**
 * @Description 定时任务调度日志表(SysJobLog)表服务实现类
 * @Author xiaoke
 * @Date 2024/8/20 11:08
 * @Version 1.0
 */
@Service
public class SysJobLogServiceImpl implements SysJobLogService {


    private final SysJobLogMapper sysJobLogMapper;

    public SysJobLogServiceImpl(SysJobLogMapper sysJobLogMapper) {
        this.sysJobLogMapper = sysJobLogMapper;
    }

    @Override
    public void create(SysJobLog sysJobLog) {
        sysJobLogMapper.insert(sysJobLog);
    }

    @Override
    public PagingVo<SysJobLog> list(ListSysJobLogDto dto) {
        //分页
        Page<SysJobLog> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<SysJobLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(!ObjectUtil.isNull(dto.getJobName()), SysJobLog::getJobName, dto.getJobName())
                .or()
                .like(!ObjectUtil.isNull(dto.getJobGroup()), SysJobLog::getJobGroup, dto.getJobGroup())
                .or()
                .like(!ObjectUtil.isNull(dto.getInvokeTarget()), SysJobLog::getInvokeTarget, dto.getInvokeTarget())
                .or()
                .eq(!ObjectUtil.isNull(dto.getStatus()), SysJobLog::getStatus, dto.getStatus())
                .or()
                .like(!ObjectUtil.isNull(dto.getInvokeTarget()), SysJobLog::getInvokeTarget, dto.getInvokeTarget())
                .or()
                .between(dto.getStartTime() != null && dto.getEndTime() != null, SysJobLog::getStartTime, dto.getStartTime(), dto.getEndTime());
        sysJobLogMapper.selectPage(page, queryWrapper);
        return new PagingVo<>(page.getRecords(), page.getTotal());
    }
}
